function [pt,x4]=rpeaks(x)

% ===================
% PARAMETERS
% ===================
plotsignals=0;
initfig=1;
% ===================

RR=RRIntervalCalculation();
clear S;
k=0;
kfig=initfig-1;

% ================================
% PREPROCESSING
% ================================
k=k+1;S{k}.tag='Raw Signal';S{k}.signal=x;
%disp(strcat('before call RR.lowpass()',num2str(length(S{k-1}.signal))));
k=k+1;S{k}.tag='After Lowpass';S{k}.signal=double(RR.lowpass(S{k-1}.signal));
k=k+1;S{k}.tag='After Bandpass';S{k}.signal=double(RR.bandpass(S{k-1}.signal));
k=k+1;S{k}.tag='After DiffSq';S{k}.signal=double(RR.diffsqfilt(S{k-1}.signal));
k=k+1;S{k}.tag='After Integration';S{k}.signal=double(RR.lpfilt(S{k-1}.signal));
k=k+1;S{k}.tag='After Clipping';S{k}.signal=double(fclip(S{k-1}.signal));
x4=S{k}.signal;

if plotsignals
    for i=1:length(S);
        kfig=initfig+i-1;
        figure(kfig);clf;
        plot(S{k}.signal);
        title(S{k}.tag);
    end;
end;

% ================================
% R-PEAK DETECTION
% ================================
y=[x4;x4];

rr=RR.findpks(y);
pt=RR.peaktimes;
pt=pt(pt~=0);
pt=double(pt);
pt=pt(pt>length(x4))-length(x4);
